package com.woniu.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu.commons.exception.YogaException;
import com.woniu.commons.model.Result;
import com.woniu.commons.util.JwtUtil;
import com.woniu.commons.util.RedisUtil;
import com.woniu.model.form.OrderPersonalClassForm;
import com.woniu.model.model.YCourseOrder;
import com.woniu.model.param.OrderPayParam;
import com.woniu.model.param.OrderPersonalClassParam;
import com.woniu.service.YCourseOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 胡歌
 * @since 2024年05月28日
 */
@RestController
@RequestMapping("/yCourseOrder")
@Api(tags = "课程订单")
public class YCourseOrderController {

    @Resource
    private RedisUtil redisUtil;
    @Resource
    private YCourseOrderService courseOrderService;

    /**
     * 团课下单
     * @param token 用户id
     * @param leagueClassId 团课id
     * @return
     */
    @GetMapping("/orderLeagueClass")
    @ApiOperation("团课下单")
    public Result<?> orderLeagueClass(@RequestHeader String token,Integer leagueClassId) {
        String refreshToken = redisUtil.get(token);
        Integer userId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("userId").toString());

        String s = courseOrderService.orderLeagueClass(userId,leagueClassId);



        return Result.newInstance(String.class).setData(s).setMsg("下单成功");
    }

    /**
     * 显示用户的订单
     * @param token
     * @return
     */
    @GetMapping("/loadClassOrderByUserId")
    @ApiOperation("加载用户的订单")
    public Result<List<YCourseOrder>> loadClassOrderByUserId(@RequestHeader String token,Integer orderState) {
        String refreshToken = redisUtil.get(token);
        Integer userId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("userId").toString());

        List<YCourseOrder> list = courseOrderService.loadClassOrderByUserId(userId,orderState);



        return Result.newListInstance(YCourseOrder.class).setMsg("查询订单成功").setData(list);
    }

    /**
     *发起支付
     * @param orderNum：订单编号
     * @param token
     * @return
     * @throws Exception
     */
    @GetMapping("/pay")
    @ApiOperation(value = "发起支付")
    public Result<?> pay(String orderNum, @RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.get(token);//从短token取出键
        int userId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("userId").toString());

        OrderPayParam orderPayParam = new OrderPayParam();
        orderPayParam.setOrderNum(orderNum);
        orderPayParam.setUserId(userId);

        String pay = courseOrderService.pay(orderPayParam);

        return Result.newInstance(String.class).setCode(200).setMsg("发起支付成功").setData(pay);
    }

    /**
     * 给支付宝回调的接口
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/notify")
    @ApiOperation(value = "给支付宝回调的接口")
    public String payNotify(HttpServletRequest request) throws Exception {
        Map<String, String> params = new HashMap<String, String>();
        Map<String, String[]> requestParams = request.getParameterMap();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            params.put(name, valueStr);
        }
        courseOrderService.payNotify(params);
        return "SUCCESS";
    }

    /**
     * 私教课下单
     * @param token 用户id
     * @param form 团课id和数量
     * @return
     */
    @PostMapping("/orderPersonalClass")
    @ApiOperation("私教课下单")
    public Result<?> orderPersonalClass(@RequestHeader String token, OrderPersonalClassForm form) {
        String refreshToken = redisUtil.get(token);
        Integer userId = Integer.parseInt(JwtUtil.parseToken(refreshToken).get("userId").toString());

        OrderPersonalClassParam param = BeanUtil.toBean(form, OrderPersonalClassParam.class);

        param.setUserId(userId);

        String s = courseOrderService.orderPersonalClass(param);


        return Result.newInstance(String.class).setData(s).setMsg("下单成功");
    }
}

